package cn.lovefix.user.controller;

import cn.lovefix.base.pageList;
import cn.lovefix.user.domain.User;
import cn.lovefix.user.query.queryByname;
import cn.lovefix.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    //登录
    @RequestMapping("/login")
    public String login(@RequestBody User user, HttpSession session){
        System.out.println(user+"我在这儿");
        User user1 = userService.pick(user);
        session.setAttribute("user1",user1);
        return "ok";
    }

    //查询列表
    @RequestMapping("/list")
    @ResponseBody
    public List list(){
        List<User> list = userService.findAll();
        return list;
    }

    //删除用户

    @RequestMapping("/del")
    public String delete(@RequestParam Long id){
        System.out.println(id);
        userService.del(id);
        return "ok";
    }
    //新增或者修改用户
    @RequestMapping("/update")
    public String addOrUpdate(@RequestBody User user){
        System.out.println(user.getDeptId()+"我是部门id");
        if (user.getId()==null){
            userService.add(user);
        }else {
            userService.update(user);
        }
        return "ok";
    }
    @ResponseBody
    @RequestMapping("/query")
    public pageList page(@RequestBody queryByname query){
        System.out.println(query.getKeyWord()+"我是keyWord");
        pageList<User> list = userService.query(query);
        return list;
    }
}
